#
#      Copyright 2011 Miranda, Tom
#       
#       Licensed under the Apache License, Version 2.0 (the "License");
#       you may not use this file except in compliance with the License.
#       You may obtain a copy of the License at
#
#          http://www.apache.org/licenses/LICENSE-2.0
#
#       Unless required by applicable law or agreed to in writing, software
#       distributed under the License is distributed on an "AS IS" BASIS,
#       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#       See the License for the specific language governing permissions and
#       limitations under the License.
#

import mc, config

# Builds a ListItems Object suitable for displaying in the filter dialog.
def getFilterItems():
	filters = ['Archived', 'Not Archived', 'Favorite', 'Not Favorite', 'Manual Recording', 'Not Manual Recording', 'Manual or Favorite', 'Not Manual or Favorite', 'Watched', 'Not Watched', 'Like', 'Don\'t Like', 'First Run', 'Re-Run', 'HDTV', 'Not HDTV']
	items = mc.ListItems()
	include = True
	for filter in filters:
		item = mc.ListItem(mc.ListItem.MEDIA_UNKNOWN)
		item.SetLabel(filter)
		method = getMethodForItem(item)		
		if include:
			item.SetProperty('IsInclude', 'True')			
			if config.doesIncludeFilterContain(method):
				item.SetProperty('IsSelected', 'True')
			else:
				item.SetProperty('IsSelected', '')
		else:
			item.SetProperty('IsExclude', 'True')
			if config.doesExcludeFilterContain(method):
				item.SetProperty('IsSelected', 'True')
			else:
				item.SetProperty('IsSelected', '')				
		include = not include
		items.append(item)
	return items

# Check if the filter represented by item is selected or not.	
def isFilterSelected(item):
	method = getMethodForItem(item)
	if item.GetProperty('IsInclude') != '':
		return config.doesIncludeFilterContain(method)
	else:
		return config.doesExcludeFilterContain(method)

# Activate the filter.	Automatically deactivates the reciprocal filter.
def addFilter(item):
	method = getMethodForItem(item)
	if item.GetProperty('IsInclude') != '':
		config.removeFromExcludeFilter(method)
		return config.addToIncludeFilter(method)
	else:
		config.removeFromIncludeFilter(method)
		return config.addToExcludeFilter(method)
		
# De-activate the filter.	
def removeFilter(item):
	method = getMethodForItem(item)
	if item.GetProperty('IsInclude') != '':
		return config.removeFromIncludeFilter(method)
	else:
		return config.removeFromExcludeFilter(method)
	
# Toggle the state of the filter.	
def toggleFilter(item, reciprocalItem):
	if isFilterSelected(item):
		item.SetProperty('IsSelected', '')
		removeFilter(item)
	else:
		item.SetProperty('IsSelected', 'True')
		reciprocalItem.SetProperty('IsSelected', '')
		addFilter(item)

# Get the Sage method that will be used to filter if this item is selected.
def getMethodForItem(item):
	return labelToMethod(item.GetLabel())
	
# Get the method for the item label.
def labelToMethod(label):
	labelDict = {'Archived':'IsLibraryFile', 'Not Archived':'IsLibraryFile', 'Favorite':'IsFavorite', 'Not Favorite':'IsFavorite', 'Manual Recording':'IsManualRecord', 'Not Manual Recording':'IsManualRecord', 'Manual or Favorite':'IsNotManualOrFavorite', 'Not Manual or Favorite':'IsNotManualOrFavorite', 'Watched':'IsWatched', 'Not Watched':'IsWatched', 'Like':'IsDontLike', 'Don\'t Like':'IsDontLike', 'First Run':'IsShowFirstRun', 'Re-Run':'IsShowFirstRun', 'HDTV':'IsAiringHDTV', 'Not HDTV':'IsAiringHDTV'}
	return labelDict[label]